package com.jlwl.houseservice.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.EmployeeSchedule;
import com.jlwl.houseservice.service.EmployeeScheduleService;
import com.jlwl.houseservice.vo.BatchScheduleVO;
import com.jlwl.houseservice.vo.ScheduleQueryVO;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 管理员排班控制器
 */
@RestController
@RequestMapping("/admin/schedules")
@PreAuthorize("hasRole('ADMIN')")
public class AdminScheduleController {

    @Autowired
    private EmployeeScheduleService employeeScheduleService;

    /**
     * 获取排班列表
     */
    @GetMapping
    public Result<Page<EmployeeSchedule>> list(ScheduleQueryVO queryVO) {
        Page<EmployeeSchedule> page = employeeScheduleService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取员工排班列表
     */
    @GetMapping("/employee/{employeeId}")
    public Result<List<EmployeeSchedule>> getEmployeeSchedules(
            @PathVariable Integer employeeId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        List<EmployeeSchedule> list = employeeScheduleService.getEmployeeSchedules(employeeId, startDate, endDate);
        return Result.success(list);
    }

    /**
     * 创建排班
     */
    @PostMapping
    public Result<Void> create(@RequestBody EmployeeSchedule schedule) {
        boolean success = employeeScheduleService.createSchedule(schedule);
        if (!success) {
            return Result.error("创建排班失败");
        }
        return Result.success();
    }

    /**
     * 更新排班
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Integer id, @RequestBody EmployeeSchedule schedule) {
        schedule.setId(id);
        boolean success = employeeScheduleService.updateSchedule(schedule);
        if (!success) {
            return Result.error("更新排班失败");
        }
        return Result.success();
    }

    /**
     * 删除排班
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean success = employeeScheduleService.deleteSchedule(id);
        if (!success) {
            return Result.error("删除排班失败");
        }
        return Result.success();
    }

    /**
     * 批量设置排班
     */
    @PostMapping("/batch")
    public Result<Void> batchSetSchedule(@Valid @RequestBody BatchScheduleVO batchScheduleVO) {
        boolean success = employeeScheduleService.batchSetSchedule(batchScheduleVO);
        if (!success) {
            return Result.error("批量设置排班失败");
        }
        return Result.success();
    }

    /**
     * 更新排班状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        boolean success = employeeScheduleService.updateScheduleStatus(id, status);
        if (!success) {
            return Result.error("更新排班状态失败");
        }
        return Result.success();
    }

    /**
     * 分配订单到排班
     */
    @PutMapping("/{id}/assign")
    public Result<Void> assignOrder(@PathVariable Integer id, @RequestParam String orderNo) {
        boolean success = employeeScheduleService.assignOrderToSchedule(id, orderNo);
        if (!success) {
            return Result.error("分配订单失败");
        }
        return Result.success();
    }

    /**
     * 取消排班的订单分配
     */
    @PutMapping("/{id}/cancel-assign")
    public Result<Void> cancelOrderAssignment(@PathVariable Integer id) {
        boolean success = employeeScheduleService.cancelOrderAssignment(id);
        if (!success) {
            return Result.error("取消订单分配失败");
        }
        return Result.success();
    }

    /**
     * 获取可用员工列表
     */
    @GetMapping("/available-employees")
    public Result<List<Map<String, Object>>> getAvailableEmployees(
            @RequestParam String jobType,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
            @RequestParam String timeSlot) {
        List<Map<String, Object>> list = employeeScheduleService.getAvailableEmployees(jobType, date, timeSlot);
        return Result.success(list);
    }
} 